package cn.sciento.boot.api.customize.service;

import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import cn.sciento.boot.api.customize.commons.repository.ApiCustomizeRepository;
import cn.sciento.boot.api.customize.commons.vo.ApiCustomizePoint;
import cn.sciento.boot.api.customize.commons.vo.ApiCustomizeRule;
import cn.sciento.boot.api.customize.generator.CodeGenerator;

public abstract class ApiCustomizeService {
  private final ApiCustomizeRepository apiCustomizeRepository;
  
  private final CodeGenerator codeGenerator;
  
  public ApiCustomizeService(ApiCustomizeRepository apiCustomizeRepository, CodeGenerator codeGenerator) {
    this.apiCustomizeRepository = apiCustomizeRepository;
    this.codeGenerator = codeGenerator;
  }
  
  public void customize(String serviceName) {
    customize(serviceName, null);
  }
  
  public void customize(String serviceName, List<String> ruleCodes) {
    List<ApiCustomizePoint> points = this.apiCustomizeRepository.getServicePoints(serviceName, ruleCodes);
    if (CollectionUtils.isEmpty(points)) {
      return;
    }
    for (ApiCustomizePoint point : points) {
      List<ApiCustomizeRule> rules = this.apiCustomizeRepository.getRule(point.getRuleCodes());
      if (CollectionUtils.isNotEmpty(rules)) {
        applyRule(point, rules);
      }
    } 
  }
  
  protected abstract void applyRule(ApiCustomizePoint paramApiCustomizePoint, List<ApiCustomizeRule> paramList);
  
  public ApiCustomizeRepository getApiCustomizeRepository() {
    return this.apiCustomizeRepository;
  }
  
  public CodeGenerator getCodeGenerator() {
    return this.codeGenerator;
  }
}
